package com.zut.base.common.util;

import com.alibaba.druid.support.json.JSONUtils;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import org.apache.commons.mail.EmailException;
import org.apache.commons.mail.SimpleEmail;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/*短信验证码工具*/
public class CodeUtils {

    private final static String regionId = "cn-hangzhou";

    private final static String accessKeyId = "LTAI5tHdBUMdgA3hPxS232kz";

    private final static String secret = "nQF8FRljRu0jHmqzDzmlkyfwjqk2E3";

    //手机验证码
    public static Map sendPhoneCode(String phone) {
        Map<String, Object> rs = new HashMap<>();
        //连接aliyun regionId不能动，AccessKeyId和secret是用户的账号和密码
        DefaultProfile profile = DefaultProfile.getProfile(regionId, accessKeyId, secret);
        //构建客户端
        IAcsClient client = new DefaultAcsClient(profile);
        //构建请求
        CommonRequest request = new CommonRequest();
        request.setSysMethod(MethodType.POST);//请求方式
        request.setSysDomain("dysmsapi.aliyuncs.com");//官方推荐不能动
        request.setSysVersion("2017-05-25");//官方推荐不能动
        //相当于一个时间，时间名称添加一个短信
        request.setSysAction("SendSms");
        //自定义参数(如：手机号，验证码，签名，模板)
        request.putQueryParameter("PhoneNumbers", phone);//手机号
        request.putQueryParameter("SignName", "攸米科技");//签名
        request.putQueryParameter("TemplateCode", "SMS_187560870");//模板
        //构建一个验证码
        HashMap<String, Object> map = new HashMap<String, Object>();
        int codeInt = (int) ((Math.random() * 9 + 1) * 100000);
        map.put("code", codeInt);
        request.putQueryParameter("TemplateParam", JSONObject.toJSONString(map));
        try {
            CommonResponse response = client.getCommonResponse(request);
            Map parse = (Map) JSONUtils.parse(response.getData());
            if ("OK".equals(parse.get("Code"))) {
                rs.put("msg", "OK");
                rs.put("code", codeInt + "");
            }
        } catch (ServerException e) {
            e.printStackTrace();
        } catch (ClientException e) {
            e.printStackTrace();
        }

        return rs;
    }

    //邮箱验证
    public static Map sendEmailCode(String emails) {
        HashMap<String, Object> map = new HashMap<String, Object>();
        try {
            SimpleEmail email = new SimpleEmail();
            email.setHostName("SMTP.163.com");
            email.setAuthentication("15738258573@163.com", "a528243772");//邮件服务器验证：用户名/密码
            email.setCharset("UTF-8");// 必须放在前面，否则乱码
            email.addTo(emails);
            email.setFrom("15738258573@163.com", "项目接口管理平台");
            email.setSubject("邮箱激活验证");
            //构建一个验证码
            int codeInt = (int) ((Math.random() * 9 + 1) * 100000);
            //短信消息！
            StringBuilder msgInfo = new StringBuilder();
            msgInfo.append("您注册的邮箱验证激活码为：" + codeInt +"。验证码有效时间为5分钟，如非你本人操作，请忽略此邮件。").append("\r\n\t");
            email.setMsg(msgInfo.toString());
            email.send();
            map.put("code", codeInt+"");
        } catch (EmailException e) {
            e.printStackTrace();
        }
        return map;
    }
}
